﻿namespace com.stifel.UserTestDao
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using com.stifel.Extensions.SharePointExtensions;
    using com.stifel.SharePoint;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Linq;

    using Context = UserTestDataContext;

    public class AddressDao : BaseDao
    {
        #region Fields

        public const string ListName = "AddressTest";

        #endregion Fields

        #region Constructors

        public AddressDao( )
            : base(ListName)
        {
            // set to the site collection url
            CurrentWebUrl = SPContext.Current.Site.RootWeb.Url;
        }

        public AddressDao( string currentWebUrl )
            : base(ListName)
        {
            // set to the site collection url
            using ( SPSite spSite = new SPSite( currentWebUrl ) )
            {
                SPWeb spWeb = spSite.RootWeb;
                {
                    CurrentWebUrl = spWeb.Url;
                }
            }
        }

        #endregion Constructors

        #region Methods

        public void Add( UserTest value )
        {
            SPList spList = getList( );
            SPListItem item = spList.AddItem( );
            item.SetValue<string>( "Title", value.Title );
            item.SetValue<string>( "usr_Address", value.internalAddress.ToString( ) );
            item.Web.AllowUnsafeUpdates = true;
            item.Update( );
        }

        public void Clear( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<UserTest> l = c.GetList<UserTest>( ListTitle );
                var v = ( from o in l
                          where o.Id > 0
                          select o );
                if ( v.Count( ) > 0 )
                {
                    l.DeleteAllOnSubmit( v );
                    c.SubmitChanges( );
                }
            }
        }

        public UserTest Get( string value )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<UserTest> l = c.GetList<UserTest>( ListTitle );
                UserTest v = ( from o in l
                               where o.Title.Equals( value, StringComparison.OrdinalIgnoreCase )
                               select o ).SingleOrDefault( );
                return v;
            }
        }

        public UserTest Get( int id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<UserTest> l = c.GetList<UserTest>( ListTitle );
                UserTest v = ( from o in l
                               where o.Id == id
                               select o ).SingleOrDefault( );
                return v;
            }
        }

        public List<UserTest> GetAll( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                List<UserTest> l = c.GetList<UserTest>( ListTitle ).ToList( );
                return l;
            }
        }

        public void XAdd( UserTest value )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<UserTest> l = c.GetList<UserTest>( ListTitle );

                UserTest v = clean( value );
                l.InsertOnSubmit( v );
                c.SubmitChanges( );
            }
        }

        UserTest clean( UserTest value )
        {
            return new UserTest
            {
                internalAddress = value.internalAddress,
                Title = value.Title
            };
        }

        SPList getList( )
        {
            return getList( true );
        }

        SPList getList( bool throwExceptionOnError )
        {
            bool disposed = false;
            SPSite currentSite = new SPSite( CurrentWebUrl );
            SPWeb spWeb = currentSite.OpenWeb( );
            string name = ListName;
            SPList spList = null;
            while ( spList == null )
            {
                foreach ( SPList list in spWeb.Lists ) if ( list.RootFolder.Name.Equals( ListName, StringComparison.OrdinalIgnoreCase ) )
                        spList = list;
            }
            if ( !disposed )
                spWeb.Dispose( );
            if ( spList == null && throwExceptionOnError )
                //throw new ListNotFoundException( name );
                throw new Exception( );

            return spList;
        }

        #endregion Methods
    }
}